﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections;
using System.Linq;
using System.IO;
using SharePointAccessMonitor.Models;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading;
using System.Web;
using System.Diagnostics;

namespace SharePointAccessMonitor.Tests
{
    [TestClass]
    public class SenarioTest
    {
        [TestMethod]
        public void LoadTest()
        {
            var rnd = new Random();
            var testFile = "test.log";
            //var httpUrl = "http://localhost:65530/Log/Write?group=proaxiaconsulting-sharepoint-com";
            var httpUrl = "http://sharepointaccessmonitor.azurewebsites.net/Log/Write?group=proaxiaconsulting-sharepoint-com";
            var testUser = new string[] { "aehara@sample.com", "tyamada@sample.com", "hsuzuki@sample.com", "atanaka@sample.com", "ksuszuki@sample.com", };
            var testData = File.ReadAllLines(testFile).Select(x=>LogItem.Parse(x)).ToList();
            Enumerable.Range(0, 1000).AsParallel().WithDegreeOfParallelism(1).ForAll((x) => {
                using(var client = new HttpClient())
                {
                    var testIdx = rnd.Next(testData.Count);
                    var content = new FormUrlEncodedContent(new Dictionary<string, string>
                    {
                        { "user", testUser[rnd.Next(testUser.Length)]},
                        { "url", testData[testIdx].Url },
                        { "title", testData[testIdx].Title },
                        { "addinfo",  testData[testIdx].AddInfo }
                    });
                    var result = client.PostAsync(httpUrl, content).Result;
                    Debug.WriteLine(result.StatusCode);
                }
                Thread.Sleep((int)(1000 * rnd.NextDouble()));
            });
        }
    }
}
